package com.base.cn.platform.os.controller.mobile.course.user;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学员课程Controller
 *
 * @author s.li
 * @create 2018-06-08-10:50
 */
@Controller
@RequestMapping("/mobile/uc/course")
public class MobileUserCourseController extends MobileBaseController {

    @Autowired
    private CourseBuyRecordService courseBuyRecordService;
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private CourseService courseService;
    /**
     * 我的课程
     * @return
     */
    @RequestMapping("/myCourse")
    public String myCourse() {
        return resultPage("/course/user/user-course-list");
    }

    /**
     * 我的课程列表
     * @param model
     * @return
     */
    @RequestMapping("/ajax/myCourse")
    public String ajaxMyCourse(HttpServletRequest request,
                               Model model,
                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("courseStatus", OrderStatus.SUCCESS);
        Map<String,Object> dataMap = courseBuyRecordService.findCourseBuyRecordFrontPage(params,currentPage,10,true,true,true,true,false,false,false);
        model.addAttribute("dataMap",dataMap);
        int total = Integer.parseInt(dataMap.get("total").toString());
        if(total <=0){//如果用户没有购买过课程，则向用户推荐课程
            //获取用户的专业ID串
            String subjectIds = cusUserService.findUserAllSubjectIds(userId);
            params = new HashMap<>();
            params.put("subjectIds",subjectIds);
            params.put("count",5);
            params.put("status",1);
            params.put("notCourseTypeKeys","'LIVE'");
            List<Map<String,Object>> courseList = courseService.findCourseList(params,true,false,false,true);
            model.addAttribute("courseList",courseList);
        }
        return resultPage("/course/user/ajax-user-course-list");
    }

    /**
     * 初始化激活套餐页面
     * @param model Model
     * @param courseId 课程ID
     * @param recordId 课程购买记录ID
     * @return 操作页面
     */
    @RequestMapping("/initActivationPackage")
    public String initActivationPackage(Model model,@RequestParam("courseId") BigDecimal courseId,
                                        @RequestParam("recordId") BigDecimal recordId){
        model.addAttribute("recordId",recordId);
        model.addAttribute("courseId",courseId);
        return resultPage("/course/package/active-pack-course");
    }

    /**
     * 获取要激活的套餐下课程的数据
     * @param recordId 购买记录
     * @return Map<String,Object>
     */
    @RequestMapping("/getActivationPackage")
    @ResponseBody
    public Map<String,Object> getActivationPackage(HttpServletRequest request,
                                                   @RequestParam("recordId") BigDecimal recordId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return courseBuyRecordService.findPackageActivationById(recordId,userId);
    }

    /**
     * 执行激活套餐课程
     * @param recordId 购买记录ID
     * @param courseIds 选择的课程ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/activationPackage")
    @ResponseBody
    public Map<String,Object> activationPackage(HttpServletRequest request,
                                                @RequestParam("recordId") BigDecimal recordId,
                                                @RequestParam("courseIds") String courseIds){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return courseBuyRecordService.activationPackage(userId,recordId,courseIds);
    }

}
